package com.jc.oms.controller.order;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jc.common.util.WebUtils;
import com.jc.ds.base.service.BaseServiceException;
import com.jc.ds.model.OrderProducts;
import com.jc.service.order.XorderService;
import com.jc.service.orderproduct.OrderProductsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.jc.business.order.XorderBusiness;
import com.jc.business.systemloglist.SystemLogListBusiness;
import com.jc.common.util.ExceptionUtil;
import com.jc.oms.controller.BaseController;
import com.jc.service.api.ParamModel;
import com.jc.service.api.yx163.order.YanXuanOrderService;
import com.jc.service.model.yx.YanXuanResult;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("order")
public class XorderController extends BaseController {

    @Autowired
    XorderBusiness xorderBusiness;
    @Autowired
    SystemLogListBusiness systemLogListBusiness;

    @Autowired
    YanXuanOrderService yanXuanOrderService;


    @RequestMapping(value = "push_order_jg")
    public @ResponseBody
    Map<String, Object> push_order_jg() {
        Map<String, Object> _ret = new HashMap<String, Object>();
        try {
            xorderBusiness.pushOrder();
            _ret.put("success", true);
        } catch (Exception e) {
            _ret.put("success", false);
            _ret.put("errorMsg", ExceptionUtil.subExceptionMessage(e, 0, 100));
        }

        return _ret;

    }


    @RequestMapping(value = "push_order_jg2", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> push_order_jg2() {
        Map<String, Object> _ret = new HashMap<String, Object>();
        try {
            xorderBusiness.pushOrder();
            _ret.put("success", true);
        } catch (Exception e) {
            getLoger().error("向严选推送订单错误 : {},{}", e.getMessage(), e);
            _ret.put("success", false);
            _ret.put("errorMsg", ExceptionUtil.subExceptionMessage(e, 0, 100));
        }

        return _ret;

    }

    /****
     *
     * 注册网易严选回调接口，用来告诉严选，哪些业务接口需要有回调
     * @Title: reg_163_method
     * @Description: TODO
     * @param @return
     * @return Map<String   ,   Object>
     * @throws
     */
    @RequestMapping(value = "reg_163_method", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> reg_163_method() {
        Map<String, Object> _ret = new HashMap<String, Object>();
        try {
            YanXuanResult ret = yanXuanOrderService.regYanXuanApiCallBack();
            if (ret != null && StringUtils.equals("200", ret.getCode())) {
                _ret.put("success", true);
            } else {
                _ret.put("success", false);
                _ret.put("errorMsg", JSONObject.toJSONString(ret.getMessage()));
            }
        } catch (Exception e) {
            _ret.put("success", false);
            _ret.put("errorMsg", ExceptionUtil.subExceptionMessage(e, 0, 100));
        }

        return _ret;

    }

    @RequestMapping(value = "reg_163_product_fields", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> reg_163_product_fields() {
        Map<String, Object> _ret = new HashMap<String, Object>();
        try {
            YanXuanResult ret = yanXuanOrderService.regYanXuanApiProductCallBackField();
            if (ret != null && StringUtils.equals("200", ret.getCode())) {
                _ret.put("success", true);
            } else {
                _ret.put("success", false);
                _ret.put("errorMsg", JSONObject.toJSONString(ret.getMessage()));
            }
        } catch (Exception e) {
            _ret.put("success", false);
            _ret.put("errorMsg", ExceptionUtil.subExceptionMessage(e, 0, 100));
        }

        return _ret;

    }


    @RequestMapping(value = "call_back_exe", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> call_back_exe(@RequestBody Map<String, Object> paramMap) {
        Map<String, Object> _ret = new HashMap<String, Object>();
        try {
            if (paramMap == null || (paramMap.get("channelOrderId") == null) || StringUtils.isBlank(paramMap.get("channelOrderId").toString())) {
                _ret.put("success", false);
                _ret.put("errorMsg", "缺少参数");
                return _ret;
            }
            YanXuanResult ret = yanXuanOrderService.exeCallBack((String) paramMap.get("channelOrderId"));
            if (ret != null && StringUtils.equals("200", ret.getCode())) {
                _ret.put("success", true);
            } else {
                _ret.put("success", false);
                _ret.put("errorMsg", JSONObject.toJSONString(ret.getMessage()));
            }
        } catch (Exception e) {
            getLoger().error("请求网易测试网关错误 : {},{}", e.getMessage(), e);
            _ret.put("success", false);
            _ret.put("errorMsg", ExceptionUtil.subExceptionMessage(e, 0, 100));
        }
        return _ret;

    }


    @RequestMapping(value = "canel_order_163", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> canel_order_163(@RequestBody Map<String, Object> paramMap) {

        Map<String, Object> _ret = new HashMap<String, Object>();
        try {
            if (paramMap == null || (paramMap.get("channelOrderId") == null) || StringUtils.isBlank(paramMap.get("orderId").toString())) {
                _ret.put("success", false);
                _ret.put("errorMsg", "缺少参数");
                return _ret;
            }
            //getLoger().error("channelOrderId : " + paramMap.get("channelOrderId").toString());
            YanXuanResult ret = yanXuanOrderService.canelOrderByYanXuan((String) paramMap.get("orderId"));
            if (ret != null && StringUtils.equals("200", ret.getCode())) {
                _ret.put("success", true);
            } else {
                _ret.put("success", false);
                _ret.put("errorMsg", JSONObject.toJSONString(ret.getMessage()));
            }
        } catch (Exception e) {
            getLoger().error("请求取消网易订单错误 : {},{}", e.getMessage(), e);
            _ret.put("success", false);
            _ret.put("errorMsg", ExceptionUtil.subExceptionMessage(e, 0, 100));
        }

        return _ret;

    }


    @RequestMapping(value = "call_back_express_exe", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> exeExpressCallBack(@RequestBody Map<String, Object> paramMap) {
        Map<String, Object> _ret = new HashMap<String, Object>();
        try {
            if (paramMap == null || (paramMap.get("channelOrderId") == null) || StringUtils.isBlank(paramMap.get("channelOrderId").toString())) {
                _ret.put("success", false);
                _ret.put("errorMsg", "缺少参数");
                return _ret;
            }
            YanXuanResult ret = yanXuanOrderService.exeExpressCallBack((String) paramMap.get("channelOrderId"));
            if (ret != null && StringUtils.equals("200", ret.getCode())) {
                _ret.put("success", true);
            } else {
                _ret.put("success", false);
                _ret.put("errorMsg", JSONObject.toJSONString(ret.getMessage()));
            }
        } catch (Exception e) {
            getLoger().error("请求网易测试网关错误 : {},{}", e.getMessage(), e);
            _ret.put("success", false);
            _ret.put("errorMsg", ExceptionUtil.subExceptionMessage(e, 0, 100));
        }
        return _ret;

    }

    @Autowired
    private XorderService xorderService;
    @Autowired
    private OrderProductsService orderProductsService;

    /**
     * 根据orderId 查询orderProducts表
     * **/
    @RequestMapping(value = "queryOrderProByOrderIds", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryOrderProByOrderIds(HttpServletRequest request) {
        Map<String, Object> _ret = new HashMap<String, Object>();
        _ret.put("code",10000);
        Map<String, Object> paramMap;
        try {
            paramMap = WebUtils.ReadAsChars(request);
            if (paramMap.get("orderId") == null){
                throw new BaseServiceException("必传参数为空 orderId");
            }
            List<OrderProducts> orderProducts = orderProductsService.queryAll(new OrderProducts() {{
                setOrderId(Long.valueOf(paramMap.get("orderId").toString()));
            }});
            _ret.put("data",orderProducts);
        } catch (Exception e) {
            getLoger().error("查询orderProducts表出错 : {},{}", e.getMessage(), e);
            _ret.put("code",9999);
            _ret.put("msg", ExceptionUtil.subExceptionMessage(e, 0, 100));
        }
        return _ret;
    }
}
